//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.RegistryManagerModule
{
    using System.Collections.ObjectModel;
    using System.Windows;
    using ManagementStudioServices.Interfaces;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
    using Microsoft.Practices.Unity;
    using Microsoft.Research.DataLayer;
    using System;

    public class SelectUserPresenter
    {

        private SelectUser selectUserWindow;

        /// <summary>
        /// Add User command.
        /// </summary>
        public DelegateCommand<object> OkCommand { get; set; }

        /// <summary>
        /// Cancel User command.
        /// </summary>
        public DelegateCommand<object> CancelCommand { get; set; }

        /// <summary>
        /// Test User command.
        /// </summary>
        //public DelegateCommand<object> SearchCommand { get; set; }

        /// <summary>
        /// Show Add User command.
        /// </summary>
        public DelegateCommand<object> ShowCommand { get; set; }

        /// <summary>
        /// Name Changes command.
        /// </summary>
        //  public DelegateCommand<object> NameChangesCommand { get; set; }

        public ObservableCollection<SelectUserModel> UserList { get; set; }


        public bool HasNoUser
        {
            get
            {
                return !HasUser;
            }
        }

        public bool HasUser
        {
            get
            {
                return this.UserList.Count > 0;
            }
        }


        public SelectUserModel SelectUserModel { get; set; }

        private IUnityContainer container;

        private IRegistryManagerService registryManagerService;

        public Guid DeleteUserId { get; set; }

        public SelectUserPresenter(Guid userId, IUnityContainer container, IRegistryManagerService service)
        {
            this.container = container;
            this.registryManagerService = service;
            this.DeleteUserId = userId;
            this.OkCommand = new DelegateCommand<object>(p => this.OkCommandExecuted(), this.CanExecuteOk);
            this.ShowCommand = new DelegateCommand<object>(p => this.ShowCommandExecuted());
            this.CancelCommand = new DelegateCommand<object>(p => this.CancelCommandExecuted());
            this.GenerateUserModel();
        }

        /// <summary>
        /// Ok Button will be disable if no selection happened
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private bool CanExecuteOk(object obj)
        {
            return this.SelectUserModel != null;
        }

        private void GenerateUserModel()
        {
            this.UserList = new ObservableCollection<SelectUserModel>();
            Collection<User> userCollection = this.registryManagerService.GetAllUserByFilter(this.DeleteUserId);
            foreach (User user in userCollection)
            {
                SelectUserModel selectModel = new SelectUserModel(user.Name, ((IObject)user).ID);
                this.UserList.Add(selectModel);
            }
        }

        /// <summary>
        /// Handler for Adding New User
        /// </summary>
        private void OkCommandExecuted()
        {
            this.CloseUserWindow();
        }

        /// <summary>
        /// Handler for Showing "Add User" Screen
        /// </summary>
        private void ShowCommandExecuted()
        {
            this.selectUserWindow = new SelectUser();
            this.selectUserWindow.Owner = (Window)this.container.Resolve<IRegionManager>("MainWindow");
            this.selectUserWindow.DataContext = this;
            this.selectUserWindow.ShowDialog();
        }

        /// <summary>
        /// Handler for Canceling Add User window
        /// </summary>
        private void CancelCommandExecuted()
        {
            this.CloseUserWindow();
            this.SelectUserModel = null;
        }

        private void CloseUserWindow()
        {
            if (this.selectUserWindow != null)
            {
                this.selectUserWindow.Close();
                this.selectUserWindow = null;
            }
        }

    }
}
